<!--   
Persistent Digital Archives and Library System (PeDALS) 
(c) 2008 - All Rights Reserved
//-->
<?php include 'includes/config.php'; 
$term = urldecode(@$_GET["prov"]);
/* query string not correct */
if ($term == "") {
	echo "Provenance not found.  You did not select a valid provenance for viewing.";
	die;
}
$params = array(&$term);	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="Persistent Digital Archives and Library System" />
<title>PeDALS Web Administration</title>
<link rel="shortcut icon" href="images/favicon.ico" />
<link href="includes/style.css" rel="stylesheet" type="text/css" />
<script>
<!--
/*Mouseover tooltip*/
function showtip(tip){
document.tool.tip.value=tip
}
//-->
</script>

</head>
<body>
<?php include 'includes\header.php'; ?>
<div id="breadcrumb-container">
  <div id="breadcrumb"><a href="index.php" title="Home page">Home</a> / <a href="provSummary.php" title="Provenances">Provenances</a> / Provenance Statistics</div>
  <div id="breadcrumb-side"><form name="search" action="search.php"><input type="text" name="q" width="50"/><input type="submit" value="Go" /></form><a href="advSearch.php" title="Advanced Search" alt="Advanced Search">Advanced Search</a>
  </div>
  <div class="clear"></div>
</div>
<!-- 
  CONTENT
  -->
<div id="content-container">
  <!-- 
	SIDE COLUMN
	-->
  <div id="content-side">
    <ul class="link-list-vertical">
      <li><a href="provSummary.php" title="View a list of all provenances in PeDALS.">List Provenances</a></li>
      <li><a href="provNew.php" title="Add a provenance to PeDALS.">Add Provenance</a></li>
    </ul>
  </div>
  <!-- 
	MAIN COLUMN
	-->
  <div id="content"><a name="maincontent" id="maincontent"></a>
  
<?php
function compare_dates($date1, $date2) {
	list($month, $day, $year) = split('/', $date1);
	$new_date1 = sprintf('%04d%02d%02d', $year, $month, $day);
	list($month, $day, $year) = split('/', $date2);
	$new_date2 = sprintf('%04d%02d%02d', $year, $month, $day);
	return ($new_date1 > $new_date2);
}
//check date if is right---------------------
if(isset($_POST['dateStart'])){
	if(empty($_POST['dateStart']) || !ereg ("^([0-9][0-2]|[0-9])\/([0-2][0-9]|3[01]|[0-9])\/[0-9]{4}|([0-9][0-2]|[0-9])-([0-2][0-9]|3[01]|[0-9])-[0-9]{4}$",$_POST['dateStart'])) {		   
		$errorMessage = "<span style=\"margin-top:-10px; color:red;\">Please enter a valid start date. (eg. mm/dd/yyyy)</span>";
		}
	else{
		if(empty($_POST['dateEnd']) || !ereg ("^([0-9][0-2]|[0-9])\/([0-2][0-9]|3[01]|[0-9])\/[0-9]{4}|([0-9][0-2]|[0-9])-([0-2][0-9]|3[01]|[0-9])-[0-9]{4}$",$_POST['dateEnd'])) {		   
		   $errorMessage = "<span style=\"margin-top:-10px; color:red;\">Please enter a valid end date. (eg. mm/dd/yyyy)</span>";
		}
		else{
			if(compare_dates($_POST['dateStart'],$_POST['dateEnd'])>0){
				$errorMessage = "<span style=\"margin-top:-10px; color:red;\"> Start date is greater than end date.</span>";
			}				
		}
	}
}
//connection to Db---------------------------
$conn = sqlsrv_connect($serverName, $connectionInfo);
//prepare query------------------------------
$proveanceId = array($_GET['prov']);	
$query="
	SELECT [Series].[SeriesId]
      		,[Series].[SeriesTitle] AS SeriesTitle
			,[Provenance].[ProvenanceName] AS ProvName
			,[SeriesAcquisition].[SeriesAcquisitionReadyToIngest] AS rti
			,[SeriesAcquisition].[SeriesAcquisitionAccessionNumber] AS AcqId
			,[SeriesAcquisition].[SeriesAcquisitionId]

			,(SELECT COUNT(*) FROM SeriesAcquisitionItem_LINK
					WHERE [SeriesAcquisitionItem_LINK].[SeriesAcquisitionId]=[SeriesAcquisition].[SeriesAcquisitionId]) AS CountAcq			
			
	FROM Series
	INNER JOIN Provenance ON [Series].[ProvenanceId]=[Provenance].[ProvenanceId]
	INNER JOIN SeriesAcquisition ON [Series].[SeriesId]=[SeriesAcquisition].[SeriesId]

	WHERE [Series].[ProvenanceId]=(?)";
	
	if(!isset($errorMessage) && isset($_POST['dateStart'])){
		$query.="AND [SeriesAcquisition].[SeriesAcquisitionReceivedDate] BETWEEN '".$_POST['dateStart']."' AND '".$_POST['dateEnd']."' ";
	}
$query.="GROUP BY [SeriesAcquisition].[SeriesAcquisitionAccessionNumber]
		,[SeriesAcquisition].[SeriesAcquisitionId]	
		,[Series].[SeriesTitle]
		,[Series].[SeriesId]
		,[Provenance].[ProvenanceName]
		,[SeriesAcquisition].[SeriesAcquisitionReadyToIngest]
	ORDER BY [Series].[SeriesTitle]";
		
$sqlData = sqlsrv_query( $conn, $query, $params);
//check if query execute---------------------
if( $sqlData === false){
     echo "Error in query preparation/execution.\n";
     die( print_r( sqlsrv_errors(), true)); 
}

//display results----------------------------
include('includes/status.php');
$ProveanceName="";
$SeriesTitle="";
$finaleOutput="";
$AcquisitionsTotal=0;
$RecordsTotal=0;
$SeriesTotal=0;
while($row = sqlsrv_fetch_array( $sqlData, SQLSRV_FETCH_ASSOC)){
	if($row['SeriesTitle']!=$SeriesTitle){
		$finaleOutput.='<tr><th colspan="4">'.$row['SeriesTitle'].'</th></tr>';
		$SeriesTitle=$row['SeriesTitle'];
		$SeriesTotal++;
		}
	$finaleOutput.='<tr><td width="20">&nbsp;</td><td align="left">'.$row['AcqId'].'</td><td width="100" align="center">'.$row['CountAcq'].' records</td>'.'<td title="'.$msgValue[$row['rti']][2].'">Status: '.$msgValue[$row['rti']][1].'</td></tr>';
	// $SeriesTotal = $row['cSer'];
	 $AcquisitionsTotal++;
	 $RecordsTotal=$RecordsTotal+$row['CountAcq'];
	 $ProveanceName = $row['ProvName'];
}
?>
    <h1><?php if(empty($ProveanceName)){echo "Provenance does not have any series, acquisitions, or records.";}else{echo $ProveanceName;} ?></h1>

<form method="post">    
<table class="table" border="1" cellspacing="0" summary="Table listing all the variant names for this provenance">
      <thead>
        <tr><th scope="col" colspan="3">Display by date: <center><?php if(isset($errorMessage)) echo $errorMessage;?></center></th></tr>
      </thead> 
      <tbody>
        <tr>
            <td style="border:none;"><label>Start Date:&nbsp;<input type="text" size="12" maxlength="12" name="dateStart" value="<?php if(isset($_POST['dateStart'])){echo $_POST['dateStart'];}?>"/></label></td>
            <td style="border:none;"><label>End Date:&nbsp;<input type="text" size="12" maxlength="12" name="dateEnd" value="<?php if(isset($_POST['dateStart'])){echo $_POST['dateEnd'];}?>"/></label></td>
            <td style="border:none;"><input type="submit" value="Show"/></label></td>
        </tr>
	</tbody>
	</table>
</form>
<p><br />Records received between <?php if(isset($_POST['dateStart']) && !isset($errorMessage)){echo $_POST['dateStart'];}else{echo '[Start Date]';}?> and <?php if(isset($_POST['dateStart']) && !isset($errorMessage)){echo $_POST['dateStart'];}else{echo '[End Date]';}?>.  Default is total.</p>

<table>
<?php if(empty($finaleOutput)){echo '<b>No results for your search. Try another dates.</b>';}else{echo $finaleOutput;}?>
</table>
<table>
<tr><td colspan="4" align="left">&nbsp;</td></tr>
<tr><td colspan="4" align="left">Number of series (total):&nbsp;<b><?php echo $SeriesTotal;?></b></td></tr>
<tr><td colspan="4" align="left">Number of acquisitions (total):&nbsp;<b><?php echo $AcquisitionsTotal;?></b></td></tr>
<tr><td colspan="4" align="left">Number of records (total):&nbsp;<b><?php echo $RecordsTotal;?></b></td></tr>
</table>

<?php include 'includes\footer.php'; ?>

</div>

</body>
</html>
